package cn.kgc.ssm.controller;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.ssm.annotations.Logs;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.enums.Operation;
import cn.kgc.ssm.service.UserService;
import cn.kgc.ssm.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 课工场
 * @date 2024/3/1
 * @description
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;


    //  用户认证
    @GetMapping("login")
    @ResponseBody
    public ResponseResult login(String username, String password, String captcha, HttpSession session){

        String code = (String) session.getAttribute("code");
        //  1. 检查验证码是否正确
        if (!code.equals(captcha)){  //  验证码错误
            ResponseResult responseResult = new ResponseResult();
            responseResult.setCode(110);
            responseResult.setMsg("验证码错误");
            return responseResult;
        }
        return userService.login(username,password,session);
    }

    // 用户的分页列表   搜索分页 和查询所有分页共用接口
    @GetMapping("page")
    @ResponseBody
    @Logs(description = Operation.SELECT,model = "用户模块")
    public ResponseResult userPageList(Integer page,Integer limit,String username){

        return userService.selectPage(page,limit,username);
    }

    // 删除
    @GetMapping("delete")
    @ResponseBody
    @Logs(description = Operation.DELETE,model = "用户模块")
    public ResponseResult deleteUser(String ids){
        // 将前端的id进行字符串的分割
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        List<Integer> collectIds = list.stream().map(id -> Integer.parseInt(id)).collect(Collectors.toList());

        return userService.deleteUsers(collectIds);
    }


    // 添加
    @PostMapping("add")
    @ResponseBody
    @Logs(description = Operation.ADD,model = "用户模块")
    public ResponseResult addUser(User user){

        // 设置用户的默认密码
        user.setPassword(SecureUtil.md5("666666"));
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        return userService.addUser(user);
    }


    //根据id获取用户信息
    @GetMapping("getById")
    @ResponseBody
    public ResponseResult selectUserById(Integer id){

        return userService.selectUserById(id);
    }


    //编辑用户信息
    @PostMapping("edit")
    @ResponseBody
    @Logs(description = Operation.UPDATE,model = "用户模块")
    public ResponseResult editUser(User user){

        user.setUpdateTime(new Date());

        return userService.editUser(user);
    }

    // 编辑个人信息
    @PostMapping("editPersonalInfo")
    @ResponseBody
    public  ResponseResult  editPersonalInfo(User user){

       return   userService.editPersonalInfo(user);
    }

    // 修改个人密码  editPersonalPassword
    @PostMapping("editPersonalPassword")
    @ResponseBody
    public  ResponseResult  editPersonalPassword(String oldPassword,String password,HttpSession session){
        return   userService.editPersonalPassword(oldPassword,password,session);
    }


    // 用户退出
    @GetMapping("logout")
    @ResponseBody
    public  ResponseResult  logout(HttpSession session){

        //   销毁当前会话
        session.invalidate();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("退出成功");
        return responseResult ;
    }
}
